package y2019.date0927.notifywaite.base;

import y2019.date0927.notifywaite.ProductConsumerModel_0;
import y2019.date0927.notifywaite.ProductConsumerModel_1;
import y2019.date0927.notifywaite.ProductConsumerModel_2;

/**
 * 生产者消费者Mode公类，实现共有测试方法。
 *
 * @author lu
 * @date 2019/9/28 0:17
 */
public class PCTestProxy<T extends IPCModel> {

    public void test(T model) {
        Class<? extends IPCModel> mclazz = model.getClass();

//        0和1共用一个测试方法
        if (ProductConsumerModel_0.class.equals(mclazz)) {
            test_0_1(model);
        } else if (ProductConsumerModel_1.class.equals(mclazz)) {
            test_0_1(model);
        } else if (ProductConsumerModel_2.class.equals(mclazz)) {
            test_2(model);
        }

    }

    private void test_2(T model) {

        new Thread(new Runnable() {
            @Override
            public void run() {
//                生产
                while (true) {
                    try {
                        model.produce();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
//                消费
                while (true) {
                    try {
                        model.consume();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    private void test_0_1(T model) {

        new Thread(new Runnable() {
            @Override
            public void run() {
//                生产
//                while (true) {
                try {
                    model.produce();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
//                消费
//                while (true) {
                try {
                    model.consume();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                }
            }
        }).start();
    }
}
